import 'package:ed25519_hd_key/ed25519_hd_key.dart';
import 'package:flutter/material.dart';
import 'package:bip39/bip39.dart' as bip39;
import 'package:hex/hex.dart';
import 'package:itoken/Components/CRouter.dart';
import 'package:itoken/Components/CStore.dart';
import 'package:itoken/Modle/MAddress.dart';
import 'package:itoken/Store/SAddress.dart';
import 'package:provider/provider.dart';
import 'package:web3dart/web3dart.dart';

class _IStore with ChangeNotifier {
  List<String> lists = [];
  String mnemonic = "";
  List<String> check = [];
  _IStore({
    @required this.lists,
    @required this.mnemonic,
  });

  removeCheck(String item) {
    lists.add(item);
    check.remove(item);
    notifyListeners();
  }

  addCheck(String item, BuildContext context) async {
    check.add(item);
    lists.remove(item);
    notifyListeners();
    if (lists.length == 0) {
      String _check = check.join(" ");
      if (_check != mnemonic) {
        print("助记词不等");
      } else {
        String seed = bip39.mnemonicToSeedHex(mnemonic);
        KeyData master = ED25519_HD_KEY.getMasterKeyFromSeed(seed);
        String privateKey = HEX.encode(master.key);
        print('私钥 ====> $privateKey');
        final private = EthPrivateKey.fromHex(privateKey);
        final address = await private.extractAddress();
        print('eth地址 ====> $address');
        // CRouter.main(
        //   address: address.toString(),
        //   private: private.toString(),
        // );

        SAddress saddress = Provider.of<SAddress>(context);
        saddress.add(MAddress.fromMap({
          "address": address.toString(),
          "private": "$privateKey",
          "mnemonic": mnemonic
        }));
        
      }
    }
  }
}

class PCheckMnemonic extends StatelessWidget {
  final String mnemonic;
  final List<String> lists;
  PCheckMnemonic({
    @required this.mnemonic,
    @required this.lists,
  });
  @override
  Widget build(BuildContext context) {
    print(mnemonic);

    return IStore(
      providers: [
        ChangeNotifierProvider(
          create: (_) => _IStore(
            lists: lists,
            mnemonic: mnemonic,
          ),
        ),
      ],
      builder: (BuildContext context) {
        _IStore store = Provider.of<_IStore>(context);

        return Scaffold(
          appBar: AppBar(
            title: Text("助记词验证"),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(20),
                  margin: EdgeInsets.all(20),
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Colors.red,
                    ),
                    borderRadius: BorderRadius.circular(6),
                  ),
                  child: Wrap(
                    children: <Widget>[
                      for (String item in store.check)
                        RaisedButton(
                          onPressed: () {
                            store.removeCheck(item);
                          },
                          child: Text(item),
                        )
                    ],
                  ),
                ),
                Wrap(
                  children: <Widget>[
                    for (String item in store.lists)
                      RaisedButton(
                        onPressed: () {
                          store.addCheck(item, context);
                        },
                        child: Text(item),
                      )
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
